﻿using Enums;
using Microsoft.Reporting.WinForms;
using System;
using System.Data;
using XF.ExControls;

namespace KYBM
{
    public partial class FrmSaleStatement : XFFormEx
    {
        private readonly XF.BLL.Base_Customer bllCustomer = new XF.BLL.Base_Customer();
        private readonly XF.BLL.Bill_DTOItems bllDTOItems = new XF.BLL.Bill_DTOItems();
        private readonly XF.BLL.Base_CheckOut bllCheckOut = new XF.BLL.Base_CheckOut();
        private XF.Model.Base_Customer customer;
        
        public FrmSaleStatement(int iCustomerId)
        {
            InitializeComponent();
            customer = bllCustomer.GetModel(iCustomerId);
        }

        private void FrmSaleStatementUnclean_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            if(customer == null)
            {
                QQMessageBox.Show(
                        this,
                        MessageText.TIP_CHOISE_CUSTOMER,
                        MessageText.MESSAGEBOX_CAPTION_TIP,
                        QQMessageBoxIcon.Information,
                        QQMessageBoxButtons.OK);
                return;
            }

            reportViewer.ProcessingMode = ProcessingMode.Local;

            LocalReport localReport = reportViewer.LocalReport;

            localReport.ReportPath = "SaleStatement.rdlc";

            DataSet dataSet = bllDTOItems.GetSaleStatement(customer.Id);

            //添加数据源
            ReportDataSource DataSet1 = new ReportDataSource();
            DataSet1.Name = "DataSet1";
            DataSet1.Value = dataSet.Tables[0];

            localReport.DataSources.Clear();
            localReport.DataSources.Add(DataSet1);

            //设置参数 
            XF.Model.Base_CheckOut firstDate = bllCheckOut.GetFirstModel(customer.Id);
            ReportParameter[] reportParameters = new ReportParameter[3];
            reportParameters[0] = new ReportParameter();
            reportParameters[0].Name = "Title";
            reportParameters[0].Values.Add("凯扬对账单-" + customer.CustomerName);
            reportParameters[1] = new ReportParameter();
            reportParameters[1].Name = "FirstDate";
            reportParameters[1].Values.Add(firstDate.CheckOutDate.ToString("yyyy-MM-dd"));
            reportParameters[2] = new ReportParameter();
            reportParameters[2].Name = "Arrears";
            reportParameters[2].Values.Add(firstDate.Arrears.ToString("0.######"));

            //添加参数
            localReport.SetParameters(reportParameters);

            // 刷新报表
            reportViewer.RefreshReport();
        }
    }
}
